package com.ldj.hibernate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class TTest.
 * 
 * @see com.ldj.hibernate.TTest
 * @author MyEclipse Persistence Tools
 */

public class TTestDAO extends BaseHibernateDAO {
	public TTestDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(TTestDAO.class);

	// property constants
	public static final String _TNAME = "TName";

	public static final String _TDESC = "TDesc";

	public void save(TTest transientInstance) {
		log.debug("saving TTest instance");
		try {
			getSession().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TTest persistentInstance) {
		log.debug("deleting TTest instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TTest findById(java.lang.String id) {
		log.debug("getting TTest instance with id: " + id);
		try {
			TTest instance = (TTest) getSession().get(
					"com.ldj.hibernate.TTest", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findAllPage() {
		log.debug("finding all Article instances");
		try {
			String queryString = "from TTest";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(10);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByExample(TTest instance) {
		log.debug("finding TTest instance by example");
		try {
			List results = getSession().createCriteria(
					"com.ldj.hibernate.TTest").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TTest instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TTest as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByTName(Object TName) {
		return findByProperty(_TNAME, TName);
	}

	public List findByTDesc(Object TDesc) {
		return findByProperty(_TDESC, TDesc);
	}

	public List findAll() {
		log.debug("finding all TTest instances");
		try {
			String queryString = "from TTest";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public TTest merge(TTest detachedInstance) {
		log.debug("merging TTest instance");
		try {
			TTest result = (TTest) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TTest instance) {
		log.debug("attaching dirty TTest instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TTest instance) {
		log.debug("attaching clean TTest instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}